package tarantula.reporter;

import java.util.Map;

import tarantula.collector.ExecResults;
import tarantula.collector.TestDataCollector;
import tarantula.instrumenter.PropertyReader;
import tarantula.instrumenter.SourcePos;
import tarantula.util.Globals;
import junit.framework.TestCase;

public class ExperimentResults extends TestCase {
	public static final String TRACE_PROPERTIES = "./tests/tarantula/reporter/tracefile.properties";
	static {
		PropertyReader.reloadProperty(TRACE_PROPERTIES);
	}
	
	public static int mathbugs = 3;
	
	public void testRunMathUnsimplifiedResult() {
		Map<SourcePos, ExecResults> rankedNum
		    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
				+ PropertyReader.getProperty("math-unsimplified-folder"));
		this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Math.");
		this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/math-unsimplified.txt",
				PropertyReader.getProperty("math-source"));
	}
	
	public void testRunMathUnsimplifiedResultByEachBug() {
		for(int i = 1; i <= mathbugs; i++) {
		  Map<SourcePos, ExecResults> rankedNum
		    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
				+ "math-unsimplified-testdata-bug" + i);
		  this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Math, bug: " + i);
		  this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/math-unsimplified-bug-" + i + ".txt",
				PropertyReader.getProperty("math-source"));
		}
	}
	
	public void testRunMathDDSliceResult() {
		Map<SourcePos, ExecResults> rankedNum
		    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
				+ PropertyReader.getProperty("math-ddslice-folder"));
		this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Math after ddlsice.");
		this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/math-ddslice.txt",
				PropertyReader.getProperty("math-source"));
	}
	
	public void testRunMathDDSliceResultByEachBug() {
		for(int i = 1; i <= mathbugs; i++) {
		  Map<SourcePos, ExecResults> rankedNum
		    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
				+ "math-ddslice-testdata-bug" + i);
		  this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Math, bug: " + i);
		  this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/math-ddslice-bug-" + i + ".txt",
				PropertyReader.getProperty("math-source"));
		}
	}
	
	public void testRunMathSimpleTest() {
		Map<SourcePos, ExecResults> rankedNum
		    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
				+ PropertyReader.getProperty("math-simpletest-folder"));
		this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Math after SimpleTest.");
		this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/math-simpletest.txt",
				PropertyReader.getProperty("math-source"));
	}
	
	public void testRunMathSimpleTestByEachBug() {
		for(int i = 1; i <= mathbugs; i++) {
			  Map<SourcePos, ExecResults> rankedNum
			    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
					+ "math-simpletest-testdata-bug" + i);
			  this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Math, bug: " + i);
			  this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/math-simpletest-bug-" + i + ".txt",
					PropertyReader.getProperty("math-source"));
			}
	}
	
	public static int primitivesnum = 2;
	
	public void testRunPrimitivesUnsimplifiedResult() {
		Map<SourcePos, ExecResults> rankedNum
	    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("primitives-unsimplified-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Primitives.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/primitives-unsimplified.txt",
	    		PropertyReader.getProperty("primitives-source"));
	}
	
	public void testRunPrimitivesUnsimplifiedResultByBugs() {
		for(int i = 1; i <= primitivesnum; i++) {
		  Map<SourcePos, ExecResults> rankedNum
	         = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			    + "primitives-unsimplified-testdata-bug" + i);
	     this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Primitives.");
	      this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/primitives-unsimplified-bug-" +
	      		i + ".txt",
	    		PropertyReader.getProperty("primitives-source"));
		}
	}
	
	public void testRunPrimitivesDDSliceResult() {
		Map<SourcePos, ExecResults> rankedNum
	    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("primitives-ddslice-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Primitives using DDSlice.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/primitives-ddslice.txt",
	    		PropertyReader.getProperty("primitives-source"));
	}
	
	public void testRunPrimitivesDDSliceResultByBugs() {
		for(int i = 1; i <= primitivesnum; i++) {
		  Map<SourcePos, ExecResults> rankedNum
	         = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			    + "primitives-ddslice-testdata-bug" + i);
	     this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Primitives.");
	      this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/primitives-ddslice-bug-" +
	      		i + ".txt",
	    		PropertyReader.getProperty("primitives-source"));
		}
	}
	
	public void testRunPrimitivesSimpleTestResult() {
		Map<SourcePos, ExecResults> rankedNum
	    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("primitives-simpletest-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Primitives using SimpleTest.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/primitives-simpletest.txt",
	    		PropertyReader.getProperty("primitives-source"));
	}
	
	public void testRunPrimitivesSimpleTestResultByBugs() {
		for(int i = 1; i <= primitivesnum; i++) {
		  Map<SourcePos, ExecResults> rankedNum
	         = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			    + "primitives-simpletest-testdata-bug" + i);
	     this.dumpResult(rankedNum, "Tarantula's results for Apache Commons Primitives.");
	      this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/primitives-simpletest-bug-" +
	      		i + ".txt",
	    		PropertyReader.getProperty("primitives-source"));
		}
	}
	
	public static int timeandmoneynum = 3;
	
	public void testRunTimeAndMoneyUnsimplifiedResult() {
		Map<SourcePos, ExecResults> rankedNum
	    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("timeandmoney-unsimplified-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for Apache Time And Money.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/timeandmoney-unsimplified.txt",
	    		PropertyReader.getProperty("timeandmoney-source"));
	}
	
	public void testRunTimeAndMoneyUnsimplifiedResultByBugs() {
		for(int i = 1; i <= timeandmoneynum; i++) {
		    Map<SourcePos, ExecResults> rankedNum
	           = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			   + "timeandmoney-unsimplified-testdata-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for Apache Time And Money.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/timeandmoney-unsimplified-bug" +
	        		+ i+ ".txt", PropertyReader.getProperty("timeandmoney-source"));
		}
	}
	
	public void testRunTimeAndMoneyDDSliceResult() {
		Map<SourcePos, ExecResults> rankedNum
	    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("timeandmoney-ddslice-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for Apache Time And Money by DDSlice.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/timeandmoney-ddslice.txt",
	    		PropertyReader.getProperty("timeandmoney-source"));
	}
	
	public void testRunTimeAndMoneyDDSliceResultByBugs() {
		for(int i = 1; i <= timeandmoneynum; i++) {
		    Map<SourcePos, ExecResults> rankedNum
	           = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			   + "timeandmoney-ddslice-testdata-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for Apache Time And Money.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/timeandmoney-ddslice-bug" +
	        		+ i+ ".txt", PropertyReader.getProperty("timeandmoney-source"));
		}
	}
	
	public void testRunTimeAndMoneySimpleTestResult() {
		Map<SourcePos, ExecResults> rankedNum
	    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("timeandmoney-simpletest-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for Apache Time And Money by SimpleTest.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/timeandmoney-simpletest.txt",
	    		PropertyReader.getProperty("timeandmoney-source"));
	}
	
	public void testRunTimeAndMoneySimpleTestResultByBugs() {
		for(int i = 1; i <= timeandmoneynum; i++) {
		    Map<SourcePos, ExecResults> rankedNum
	           = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			   + "timeandmoney-simpletest-testdata-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for Apache Time And Money.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/timeandmoney-simpletest-bug" +
	        		+ i+ ".txt", PropertyReader.getProperty("timeandmoney-source"));
		}
	}
	
	public static int beanutilsnum = 2;
	
	public void testRunBeanutilsUnsimplifiedResult() {
		Map<SourcePos, ExecResults> rankedNum
	    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("beanutils-unsimplified-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for Beanutils.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/beanutils-unsimplified.txt",
	    		PropertyReader.getProperty("beanutils-source"));
	}
	
	public void testRunBeanutilsUnsimplifiedResultByBugs() {
		for(int i = 1; i <= beanutilsnum; i++) {
		    Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			    + "beanutils-unsimplified-testdata-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for Beanutils.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/beanutils-unsimplified-bug-."
	        		+ i +".txt", PropertyReader.getProperty("beanutils-source"));
		}
	}
	
	public void testRunBeanutilsDDSliceResult() {
		Map<SourcePos, ExecResults> rankedNum
	    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("beanutils-ddslice-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for Beanutils by DDSlice.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/beanutils-ddslice.txt",
	    		PropertyReader.getProperty("beanutils-source"));
	}
	
	public void testRunBeanutilsDDSliceResultByBugs() {
		for(int i = 1; i <= beanutilsnum; i++) {
		    Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			    + "beanutils-ddslice-testdata-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for Beanutils.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/beanutils-ddslice-bug-."
	        		+ i +".txt", PropertyReader.getProperty("beanutils-source"));
		}
	}
	
	public void testRunBeanutilsSimpleTestResult() {
		Map<SourcePos, ExecResults> rankedNum
	    = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("beanutils-simpletest-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for Beanutils by SimpleTest.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/beanutils-simpletest.txt",
	    		PropertyReader.getProperty("beanutils-source"));
	}
	
	public void testRunBeanutilsSimpleTestResultByBugs() {
		for(int i = 1; i <= beanutilsnum; i++) {
		    Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			    + "beanutils-simpletest-testdata-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for Beanutils.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/beanutils-simpletest-bug-."
	        		+ i +".txt", PropertyReader.getProperty("beanutils-source"));
		}
	}
	
	public void testRunJDomUnsimplifiedResult() {
		Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("jdom-unsimplified-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for JDom.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/jdom-unsimplified.txt",
	    		null);
	}
	
    public void testRunJDomDDSliceResult() {
    	Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("jdom-ddslice-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for JDom by DDSlice.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/jdom-ddslice.txt",
	    		null);
	}

    public void testRunJDomSimpleTestResult() {
    	Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("jdom-simpletest-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for JDom by SimpleTest.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/jdom-simpletest.txt",
	    		null);
    }
    
    public static int jdknum = 2;
    
    public void testRunJDKUtilUnsimplifiedResult() {
		Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("jdkutil-unsimplified-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for jdkutil.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/jdkutil-unsimplified.txt",
	    		null);
	}
    
    public void testRunJDKUtilUnsimplifiedResultByBugs() {
    	for(int i = 1; i <= jdknum; i++) {
    		Map<SourcePos, ExecResults> rankedNum
	          = getRankedResults(PropertyReader.getProperty("jdkutils-unsimplified-trace-folder") + "-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for JDKUtil.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/jdkutil-unsimplified" +
	        		"-bug-" + i + ".txt",
	    		null);
    	}
    }
	
    public void testRunJDKUtilDDSliceResult() {
    	Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("jdkutil-ddslice-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for jdkutil by DDSlice.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/jdkutil-ddslice.txt",
	    		null);
	}
    
    public void testRunJDKUtilDDSliceResultByBugs() {
    	for(int i = 1; i <= jdknum; i++) {
    		Map<SourcePos, ExecResults> rankedNum
	          = getRankedResults(PropertyReader.getProperty("jdkutils-ddslice-trace-folder") + "-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for JDKUtil.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/jdkutil-ddslice" +
	        		"-bug-" + i + ".txt",
	    		null);
    	}
    }

    public void testRunJDKUtilSimpleTestResult() {
    	Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("jdkutil-simpletest-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for JDom by SimpleTest.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/jdkutil-simpletest.txt",
	    		null);
    }
    
    public void testRunJDKUtilSimpleSliceResultByBugs() {
    	for(int i = 1; i <= jdknum; i++) {
    		Map<SourcePos, ExecResults> rankedNum
	          = getRankedResults(PropertyReader.getProperty("jdkutils-simpletest-trace-folder") + "-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for JDKUtil.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/jdkutil-simpletest" +
	        		"-bug-" + i + ".txt",
	    		null);
    	}
    }
    
    public static int collectionsnum = 3;
    
    public void testRunCollectionsUnsimplifiedResult() {
		Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("collections-unsimplified-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for collections.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/collections-unsimplified.txt",
	    		null);
	}
    
    public void testRunCollectionsUnsimplifiedResultByBugs() {
    	for(int i = 1; i <= collectionsnum; i++) {
    		Map<SourcePos, ExecResults> rankedNum
	          = getRankedResults(PropertyReader.getProperty("collections-unsimplified-trace-folder") + "-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for collections.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/collections-unsimplified" +
	        		"-bug-" + i + ".txt",
	    		null);
    	}
	}
	
    public void testRunCollectionsDDSliceResult() {
    	Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("collections-ddslice-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for collections by DDSlice.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/collections-ddslice.txt",
	    		null);
	}
    
    public void testRunCollectionsDDSliceResultByBugs() {
    	for(int i = 1; i <= collectionsnum; i++) {
    		Map<SourcePos, ExecResults> rankedNum
	          = getRankedResults(PropertyReader.getProperty("collections-ddslice-trace-folder") + "-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for collections.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/collections-ddslice" +
	        		"-bug-" + i + ".txt",
	    		null);
    	}
	}

    public void testRunCollectionsSimpleTestResult() {
    	Map<SourcePos, ExecResults> rankedNum
	        = getRankedResults(PropertyReader.getProperty("tracefiledir") + Globals.fileSep
			+ PropertyReader.getProperty("collections-simpletest-folder"));
	    this.dumpResult(rankedNum, "Tarantula's results for collections by SimpleTest.");
	    this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/collections-simpletest.txt",
	    		null);
    }
    
    public void testRunCollectionsSimpleTestResultByBugs() {
    	for(int i = 1; i <= collectionsnum; i++) {
    		Map<SourcePos, ExecResults> rankedNum
	          = getRankedResults(PropertyReader.getProperty("collections-simpletest-trace-folder") + "-bug" + i);
	        this.dumpResult(rankedNum, "Tarantula's results for collections.");
	        this.generateReport(rankedNum, "./tests/tarantula/reporter/generatedreport/collections-simpletest" +
	        		"-bug-" + i + ".txt",
	    		null);
    	}
	}
	
	protected void dumpResult(Map<SourcePos, ExecResults> rankedNum, String message) {
		System.out.println(message);
		System.out.println("All the ranked statements based on suspiciousness: ");
		TestDataCollector.dumpCollectResult(rankedNum);
	}
	
	protected void generateReport(Map<SourcePos, ExecResults> rankedNum, String filePath, String sourcepath) {
		ReportGenerator generator = null;
		if(sourcepath != null && !sourcepath.trim().equals("") && PropertyReader.getProperty("printjimple").equals("false")) {
		    generator = new ReportGenerator(rankedNum, filePath, sourcepath);
		} else {
			generator = new ReportGenerator(rankedNum, filePath);
		}
		if(PropertyReader.getProperty("removerepeat").equals("true")) {
			generator.remove_repeat = true;
		}
		generator.generateReport();
	}
	
	protected Map<SourcePos, ExecResults> getRankedResults(String traceDir) {
		return getRankedResults(traceDir, BuggyStatementReporter.output_num); //default num
	}
	
	protected Map<SourcePos, ExecResults> getRankedResults(String traceDir, int outputNum) {
		BuggyStatementReporter.output_num = outputNum;
		BuggyStatementReporter reporter = new BuggyStatementReporter(traceDir);
		Map<SourcePos, ExecResults> rankedResults = reporter.outputRankedBuggyStatement();
		return rankedResults;
	}
}
